<!DOCTYPE html>
<html lang="en">
	<head>
		<title>three.js webgl - morph target - human</title>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
		<style>
			body {
				color: #000;
				font-family:Monospace;
				font-size:13px;
				text-align:center;

				background-color: #fff;
				margin: 0px;
				overflow: hidden;
			}

			#info {
				position: absolute;
				top: 0px; width: 100%;
				padding: 5px;
			}

			a {
				color: #0af;
			}
		</style>
	</head>

	<body>

		<div id="container"></div>

		<div id="info">
		<a href="http://github.com/mrdoob/three.js" target="_blank">three.js</a> webgl - morph targets - human
		</div>

		<script src="../build/three.js"></script>

		<script src="js/UCSCharacter.js"></script>

		<script src="js/Detector.js"></script>

		<script src='js/libs/dat.gui.min.js'></script>

		<script src="js/controls/OrbitControls.js"></script>

		<script>

			var SCREEN_WIDTH = window.innerWidth;
			var SCREEN_HEIGHT = window.innerHeight;

			var container;

			var camera, scene;
			var renderer;

			var mesh;

			var mouseX = 0, mouseY = 0;

			var windowHalfX = window.innerWidth / 2;
			var windowHalfY = window.innerHeight / 2;

			var clock = new THREE.Clock();

			var gui, skinConfig, morphConfig;

			document.addEventListener( 'mousemove', onDocumentMouseMove, false );

			init();
			animate();

			function init() {

				container = document.getElementById( 'container' );

				camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 1, 100000 );
				camera.position.set( 2000, 5000, 5000 );

				scene = new THREE.Scene();

				// LIGHTS

				var light = new THREE.DirectionalLight( 0xffffff, 1 );
				light.position.set( 0, 140, 500 );
				light.position.multiplyScalar( 1.1 );
				light.color.setHSL( 0.6, 0.075, 1 );
				scene.add( light );

				//

				var light = new THREE.DirectionalLight( 0xffffff, 1 );
				light.position.set( 0, - 1, 0 );
				scene.add( light );

				// RENDERER

				renderer = new THREE.WebGLRenderer( { antialias: true } );
				renderer.setClearColor( 0xffffff );
				renderer.setPixelRatio( window.devicePixelRatio );
				renderer.setSize( SCREEN_WIDTH, SCREEN_HEIGHT );
				container.appendChild( renderer.domElement );

				// CHARACTER

				character = new THREE.UCSCharacter();
				character.onLoadComplete = function () {

					console.log( "Load Complete" );
					console.log( character.numSkins + " skins and " + character.numMorphs + " morphtargets loaded." );
					gui = new dat.GUI();
					setupSkinsGUI();
					setupMorphsGUI();
					gui.width = 300;
					gui.open();

				};

				var loader = new THREE.FileLoader();
				loader.load( 'models/skinned/UCS_config.json', function ( text ) {

					var config = JSON.parse( text );
					character.loadParts( config );
					scene.add( character.root );

				} );

				window.addEventListener( 'resize', onWindowResize, false );

				controls = new THREE.OrbitControls( camera, renderer.domElement );
				controls.target.set( 0, 3000, 0 );

				controls.addEventListener( 'change', render );

			}

			function setupSkinsGUI() {

				var skinGui = gui.addFolder( "Skins" );

				skinConfig = {
					wireframe: false
				};

				var skinCallback = function ( index ) {

					return function () {

						character.setSkin( index );

					};

				};

				for ( var i = 0; i < character.numSkins; i ++ ) {

					var name = character.skins[ i ].name;
					skinConfig[ name ] = skinCallback( i );

				}

				for ( var i = 0; i < character.numSkins; i ++ ) {

					skinGui.add( skinConfig, character.skins[ i ].name );

				}

				skinGui.open();

			}

			function setupMorphsGUI() {

				var morphGui = gui.addFolder( "Morphs" );

				morphConfig = {
				};

				var morphCallback = function( index ) {

					return function () {

						character.updateMorphs( morphConfig );

					}

				};

				for ( var i = 0; i < character.numMorphs; i ++ ) {

					var morphName = character.morphs[ i ];
					morphConfig[ morphName ] = 0;

				}

				for ( var i = 0; i < character.numMorphs; i ++ ) {

					morphGui.add( morphConfig, character.morphs[ i ] ).min( 0 ).max( 100 ).onChange( morphCallback( i ) );

				}

				morphGui.open();

			}

			function onWindowResize() {

				windowHalfX = window.innerWidth / 2;
				windowHalfY = window.innerHeight / 2;

				camera.aspect = window.innerWidth / window.innerHeight;
				camera.updateProjectionMatrix();

				renderer.setSize( window.innerWidth, window.innerHeight );

			}

			function onDocumentMouseMove( event ) {

				mouseX = ( event.clientX - windowHalfX ) * 10;
				mouseY = ( event.clientY - windowHalfY ) * 10;

			}

			//

			function animate() {

				requestAnimationFrame( animate );

				controls.update();

				render();

			}

			function render() {

				var delta = 0.75 * clock.getDelta();

				// update skinning

				character.mixer.update( delta );

				renderer.render( scene, camera );

			}

		</script>

	</body>
</html>
